package com.chukun.gateway.core.parallel.disruptor;

import com.google.common.base.Preconditions;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.ProducerType;
import lombok.Data;

/**
 * @author chukun
 * @version 1.0.0
 * @description 创建disruptor的创建者对象
 * @createTime 2023/12/23 20:05
 */
@Data
public class DisruptorHandlerBuilder<E> {

    private ProducerType producerType = ProducerType.MULTI;

    private int bufferSize = 1024 * 16;

    private int threads =1;

    private String namePrefix = "";

    private WaitStrategy waitStrategy = new BlockingWaitStrategy();

    private EventListener<E> eventListener;




    public DisruptorHandlerBuilder<E> setProducerType(ProducerType producerType) {
        Preconditions.checkNotNull(producerType);
        this.producerType = producerType;
        return  this;
    }

    public DisruptorHandlerBuilder<E> setBufferSize(int bufferSize) {
        Preconditions.checkArgument(Integer.bitCount(bufferSize) ==1);
        this.bufferSize = bufferSize;
        return  this;
    }

    public DisruptorHandlerBuilder<E> setThreads(int threads) {
        Preconditions.checkArgument(threads>0);
        this.threads = threads;
        return  this;
    }

    public DisruptorHandlerBuilder<E> setNamePrefix(String namePrefix) {
        Preconditions.checkNotNull(namePrefix);
        this.namePrefix = namePrefix;
        return  this;
    }

    public DisruptorHandlerBuilder<E> setWaitStrategy(WaitStrategy waitStrategy) {
        Preconditions.checkNotNull(waitStrategy);
        this.waitStrategy = waitStrategy;
        return  this;
    }

    public DisruptorHandlerBuilder<E> setEventListener(EventListener<E> eventListener) {
        Preconditions.checkNotNull(eventListener);
        this.eventListener = eventListener;
        return  this;
    }

    public DisruptorParallelQueueHandler<E> builder(){
        return  new DisruptorParallelQueueHandler(this);
    }
}
